<?php

class TeamController extends RestController
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }
    /**
     * The get action handles GET requests and receives an 'id' parameter; it
     * should respond with the server resource state of the resource identified
     * by the 'id' value.
     */
     public function getAction()
     {
     	$teamid = $this->_getParam('id');
    	if($uid <> null && $this->getRequest()->isGet())
    	{
    		$sql = "select * from team where teamid=?";
    		$result = $this->getDB()->fetchRow($sql,$teamid);
    		if(!empty($result))
    		{
	    		$this->getResponse()
    				->setHttpResponseCode(200)
    				->appendBody($result);
    		}
    		else 
    		{
    			$this->getResponse()->setHttpResponseCode(400)
    				->appendBody('Your Teamid don\'t exist in the database');
    		}	
    	}
    	else
    	{
    		$this->getResponse()->setHttpResponseCode(400);    		
    	}
     }

    /**
     * The post action handles POST requests; it should accept and digest a
     * POSTed resource representation and persist the resource state.
     */
     public function postAction()
     {
     	$teamid = $this->_getParam('teamid');	
    	if(!empty($teamid) && $this->getRequest()->isPost())
    	{
    		$sql = "select teamid from team where teamid=?";
    		$result = $this->getDB()->fetchRow($sql,$teamid);    		    			
    		if(!empty($result))
    		{	    	
    			//$sql = "update user set email=? and nickname=? and firstname=? and lastname=? and password=?";
    			$this->getDB()->update('team', $this->getParameters(),'teamid=' . $teamid);	
    		}
    		else
    		{
    			$this->getDB()->insert('team', $this->getParameters());
    		}    		
    	}
     }
     
     /**
      * 
      * @var Array
      */
     private function getParameters()     
     {
     	$data = array();
		$teamid = $this->_getParam('teamid');
		if(!empty($teamid))
    	{
    		$data['teamid']= $teamid;
    	}
    	$teamname = $this->_getParam('teamname');
    	if(!empty($teamname))
    	{
    		$data['teamname']=$teamname;
    	}
    	$description = $this->_getParam('description');
    	if(!empty($description))
    	{
    		$data['description']=$description;
    	}
    	return $data;
     }

    /**
     * The put action handles PUT requests and receives an 'id' parameter; it
     * should update the server resource state of the resource identified by
     * the 'id' value.
     */
     public function putAction()
     {
     
     }

    /**
     * The delete action handles DELETE requests and receives an 'id'
     * parameter; it should update the server resource state of the resource
     * identified by the 'id' value.
     */
     public function deleteAction()
     {
     
     }
}

